"""
散客比赛相关 Schemas
"""
from pydantic import BaseModel, Field
from typing import Optional, List, Dict
from datetime import datetime
from app.schemas.common import Location


class PositionRequirement(BaseModel):
    """位置需求"""
    current: int = 0
    total: int = 0


class PositionRequirements(BaseModel):
    """所有位置需求"""
    forward: PositionRequirement = PositionRequirement()
    midfielder: PositionRequirement = PositionRequirement()
    defender: PositionRequirement = PositionRequirement()
    goalkeeper: PositionRequirement = PositionRequirement()


class MatchPlayerBase(BaseModel):
    """比赛球员基础信息"""
    userId: str
    userName: str
    avatar: Optional[str] = None
    position: str
    rating: float = 0.0


class MatchPlayerResponse(MatchPlayerBase):
    """比赛球员响应"""
    joinTime: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class MatchBase(BaseModel):
    """散客比赛基础信息"""
    venueId: str
    venueName: str
    date: str = Field(..., pattern=r"^\d{4}-\d{2}-\d{2}$")
    startTime: str = Field(..., pattern=r"^\d{2}:\d{2}$")
    endTime: str = Field(..., pattern=r"^\d{2}:\d{2}$")
    type: str = Field(..., pattern="^(11|7|5)$")
    pricePerPerson: float = Field(..., gt=0)
    maxPlayers: int = Field(..., ge=1)
    minPlayers: int = Field(1, ge=1)


class MatchCreate(MatchBase):
    """创建散客比赛"""
    publisherId: str
    publisherName: str
    latitude: Optional[float] = None
    longitude: Optional[float] = None
    address: Optional[str] = None
    positions: Optional[Dict[str, PositionRequirement]] = None
    note: Optional[str] = Field(None, max_length=500)


class MatchUpdate(BaseModel):
    """更新散客比赛"""
    maxPlayers: Optional[int] = Field(None, ge=1)
    pricePerPerson: Optional[float] = Field(None, gt=0)
    note: Optional[str] = Field(None, max_length=500)


class MatchResponse(MatchBase):
    """散客比赛响应"""
    id: str
    location: Location
    currentPlayers: int = 0
    deadline: Optional[datetime] = None
    publisherId: str
    publisherName: str
    positions: Dict[str, PositionRequirement] = {}
    note: Optional[str] = None
    players: List[MatchPlayerResponse] = []
    
    class Config:
        from_attributes = True

